﻿@page "/stockout-list"
@using Microsoft.EntityFrameworkCore
@using Wms.Core.Entities
@using Wms.Core.Extensions
@inject Wms.Data.WarehouseDbContext Db
@inject NavigationManager Navigation
@inject IJSRuntime JSRuntime

<div class="container-fluid py-4">
    <!-- 页面标题 + 新增按钮 -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="text-danger">
            <span class="oi oi-data-transfer-up me-2"></span> 出库单列表
        </h4>
        <button class="btn btn-danger btn-lg px-4" @onclick="NewOrder">
            <span class="oi oi-plus me-1"></span> 新增出库单
        </button>
    </div>

    <!-- 列表卡片 -->
    <div class="card shadow-sm">
        <div class="card-body p-0">
            @if (orders == null)
            {
                <!-- 加载中 -->
                <div class="text-center py-5">
                    <div class="spinner-border text-primary" role="status"></div>
                    <p class="mt-3 text-muted">数据加载中...</p>
                </div>
            }
            else if (!orders.Any())
            {
                <!-- 空数据 -->
                <div class="text-center py-5">
                    <img src="https://cdn.jsdelivr.net/gh/twbs/icons/icons/file-earmark-text.svg" alt="空数据" width="48" class="mb-3 opacity-50" />
                    <p class="text-muted">暂无出库单记录，点击上方按钮新增。</p>
                </div>
            }
            else
            {
                <!-- 数据表格 -->
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="table-light">
                            <tr>
                                <th scope="col">单号</th>
                                <th scope="col">日期</th>
                                <th scope="col" class="text-center">明细项数</th>
                                <th scope="col" class="text-center">总数量</th>
                                <th scope="col" class="text-center" style="width: 150px;">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var o in orders)
                            {
                                <tr>
                                    <td>
                                        <strong>@o.OrderNo</strong>
                                    </td>
                                    <td>@o.Date.ToString("yyyy-MM-dd HH:mm")</td>
                                    <td class="text-center text-primary">
                                        @(o.Details?.Count ?? 0)
                                    </td>
                                    <td class="text-center">
                                        <span class="badge bg-warning text-dark">@o.TotalQuantity()</span>
                                    </td>
                                    <td class="text-center">
                                        <button class="btn btn-sm btn-outline-primary"
                                                @onclick="() => ViewDetail(o.Id)"
                                                title="查看明细">
                                            <span class="oi oi-eye"></span> 查看
                                        </button>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            }
        </div>
    </div>

    <!-- 返回顶部按钮 -->
    <div class="mt-3 text-end">
        <button class="btn btn-sm btn-outline-secondary" @onclick="ScrollToTop">
            <span class="oi oi-arrow-thick-top"></span> 顶部
        </button>
    </div>
</div>

@code {
    private List<StockOutOrder>? orders;

    protected override async Task OnInitializedAsync()
    {
        orders = await Db.StockOutOrders
            .Include(o => o.Details)
            .ThenInclude(d => d.Item)
            .OrderByDescending(o => o.Date)
            .ToListAsync();
    }

    private void NewOrder()
    {
        Navigation.NavigateTo("/stockout");
    }

    private void ViewDetail(int id)
    {
        Navigation.NavigateTo($"/stockout-detail/{id}");
    }

    private async Task ScrollToTop()
    {
        await JSRuntime.InvokeVoidAsync("scrollTo", 0, 0);
    }
}